package com.fuluwa.domain.usercenter.model.competence;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.google.common.collect.Lists;

public class CompentenceLevelHelper {

	public static CompentenceLevel getMinCompentenceLevel(List<CompentenceLevel> lists,
			UserCompetenceScore userCompetenceScore) {

		List<CompentenceLevel> list1 = Lists.newArrayList();// get same type
															// compentenceLevel
		for (CompentenceLevel compentenceLevel : lists) {
			if (userCompetenceScore.getMinCompetenceType().equals(compentenceLevel.getCompetenceType())) {
				list1.add(compentenceLevel);
			}
		}

		// sort it
		Collections.sort(list1, new Comparator<CompentenceLevel>() {
			@Override
			public int compare(CompentenceLevel o1, CompentenceLevel o2) {
				return o1.getLevel() - o2.getLevel();
			}
		});

		CompentenceLevel ret = list1.get(0);
		for (int i = 0; i < list1.size(); i++) {
			CompentenceLevel current = list1.get(i);
			if (list1.size() == (i+1)) {
				return ret;
			}
			CompentenceLevel next = list1.get(i + 1);
			if (userCompetenceScore.getMinScore() >= current.getLevelValue()
					&& userCompetenceScore.getMinScore() <= next.getLevelValue()) {
				return current;
			}
		}
		return ret;
	}
}
